<?php
// +-------------------------------------------------------------------------------
// | DingZone [ A Rapid Development Framework For Dingtalk ]
// +-------------------------------------------------------------------------------
// | ExamController.php v1.0
// |
// | Copyright (c) 2014-2018 http://www.heimalanshi.com All rights reserved.
// | Author : rahul
// | Date : 2018/10/30
// +-------------------------------------------------------------------------------

namespace app\core\controller\site;


use app\core\common\bean\BeanController;
use app\core\common\bean\BFC;
use app\core\service\DatabaseBackupService;
use app\extend\db\TableInfoHelper;
use think\Db;
use think\Model;

class BackupController extends SiteBaseController {

    public static function _q($name) {
        return Db::name($name);
    }

    public function all() {
        return view();
    }

    public function tables() {
        $ret = [];
        $tables = TableInfoHelper::getTables();
        
        $i = 1;
        foreach ($tables as $table) {
            if (substr($table, count($table)-6, 5) == '_view') continue;
            $lineCount = $this->_q($table)->count();
            $ret[] = [
                'tablename' => $table,
                'lineCount' => $lineCount,
                'state' => '尚未备份',
                'id' => $i++,
            ];
        }
        return [
            'code' => 0,
            'count' => count($ret),
            'data' => $ret,
        ];
    }

    public function backupTable() {
        $tablename = input('post.tablename');
        $ith = input('post.ith');
        $ret = [];
        try {
            DatabaseBackupService::backupTable($tablename);
            $ret = [
                'i' => $ith + 1,
                'bg' => 'green',
                'state' => '备份成功'
            ];
        } catch (Exception $e) {
            $ret = [
                'i' => $ith + 1,
                'bg' => 'red',
                'state' => '备份失败'
            ];
        }
        return $ret;
    }
}                 